﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using Newtonsoft.Json.Linq;

namespace SAP.People
{
    class CrawlTask : ITask
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private SpiderContext context;

        public CrawlTask(SpiderContext ctx)
        {
            context = ctx;
        }

        public void RunSynchronously()
        {
            context.Employees.AsParallel().ForAll(LoadDetail);
        }

        private void LoadDetail(Employee p)
        {
            var json = Path.Combine(context.IntermediateDir, p.UserId + ".json");
            var html = Path.Combine(context.IntermediateDir, p.UserId + ".html");

            if (File.Exists(json))
                return;

            if (!File.Exists(html))
            {
                LoadHtml(p, html);
                log.InfoFormat("Retrieving {0} - {1}", p.UserId, p.FullName);
            }

            ExtractData(p, html, json);
            log.InfoFormat("Extracting {0} - {1}", p.UserId, p.FullName);
        }

        private void ExtractData(Employee p, string html, string json)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(File.ReadAllText(html));

            var tables = doc.DocumentNode.SelectNodes("//div[@class='pbody puserinfo']/table/tbody");
            var personal = tables[0];
            var orgnize = tables[1];
            var communication = tables[2];

            p.Alias = personal.SelectNodes("tr/td")[0].InnerText.Trim();
            p.JobTitle = personal.SelectNodes("tr/td")[3].InnerText.Trim();
            p.JobType = personal.SelectNodes("tr/td")[4].InnerText.Trim();

            p.Email = communication.SelectNodes("tr/td")[0].InnerText.Trim();
            p.Mobile = communication.SelectNodes("tr/td")[2].InnerText.Trim();

            p.GradeLevel = GradeUtil.Map(p.JobType);

            var data = JObject.FromObject(p).ToString();
            File.WriteAllText(json, data);
        }

        private void LoadHtml(Employee p, string html)
        {
            var url = string.Format("https://performancemanager5.successfactors.eu/xi/ui/edu/pages/prevewProfile.xhtml?selected_user={0}&selected_view=43", p.UserId);
            var cookie = context.Cookie;

            using (WebClient wc = new WebClient())
            {
                wc.Headers.Add("Cookie", cookie);
                wc.DownloadFile(url, html);
            }
        }
    }
}
